package com.cos.shop.dao.impl;

import com.cos.shop.dao.OrdersDao;
import com.cos.shop.entity.Orders;
import com.cos.shop.entity.vo.OrdersVO;
import com.cos.shop.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: OrdersDaoImpl
 * Author: saber
 * Date:2022/9/20 12:36
 */
public class OrdersDaoImpl implements OrdersDao {
    private QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

    @Override
    public int addOrders(Orders orders)throws SQLException {
        return qr.update("insert into orders values(?,?,?,?,?,?)",
                orders.getOid(),orders.getUid(),orders.getAid(),
                orders.getOcount(),orders.getOtime(),orders.getOstate());
    }

    @Override
    public List<OrdersVO> getAllByUid(int uid) throws SQLException {
        return qr.query("select * from orders o inner join address a on o.aid = a.aid where o.uid = ?",
                new BeanListHandler<>(OrdersVO.class),uid);
    }

    @Override
    public OrdersVO getOrdersByOid(String oid)throws SQLException {
        return qr.query("select * from orders o inner join address a on o.aid = a.aid where oid = ?"
                ,new BeanHandler<>(OrdersVO.class),oid);
    }

    @Override
    public int changeStatus(String oid, int encomment)throws SQLException  {
        return qr.update("update orders set ostate = ? where oid = ?",
                encomment,oid);
    }
}
